package com.bawei.lib_common.utils;

import android.annotation.SuppressLint;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 作者：lxy
 * 功能:
 * 时间：2025/5/25
 */
public class WeekDayUtil {

    /**
     * 根据日期返回周几
     * @param date
     * @return
     */
    public static String getWeek(String date) {
        return getWeekDay(getWeekDay(date));
    }

    public static int getWeekDay(String date) {
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, 7));
        int day = Integer.parseInt(date.substring(8, 10));
        //获得一个日历
        Calendar calendar = Calendar.getInstance();
        //设置当前时间,月份是从0月开始计算
        calendar.set(year, month - 1, day);
        //星期表示1-7，是从星期日开始
        return calendar.get(Calendar.DAY_OF_WEEK);
    }



    private static String getWeekDay(int dayForWeek) {

        if (dayForWeek == 1) {
            return "周日";
        } else if (dayForWeek == 2) {
            return "周一";
        } else if (dayForWeek == 3) {
            return "周二";
        } else if (dayForWeek == 4) {
            return "周三";
        } else if (dayForWeek == 5) {
            return "周四";
        } else if (dayForWeek == 6) {
            return "周五";
        } else if (dayForWeek == 7) {
            return "周六";
        } else {
            return "";
        }
    }

    /**
     * 日期转为几月几日
     * @param date
     * @return
     */
    public static String formatDate(String date) {

        String dateFormat = null;
        try {
            dateFormat = date.substring(5, 7) + "月" + date.substring(8, 10) + "日";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateFormat;
    }

    @SuppressLint("SimpleDateFormat")
    public static String getTime(long date) {

        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss");
        return format.format(date);
    }

    /**
     * flag：正、负数，正数表示向后加flag个月，负数表示向前减flag个月。
     *
     * @param mon
     * @param flag
     * @return
     */
    public static Long nextMonth(Long mon, int flag) {
        Date date = mon == null ? new Date() : new Date(mon);//当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);//设置当前日期
        calendar.add(Calendar.MONTH, flag);
        return calendar.getTime().getTime();
    }

    /**
     * flag：正、负数，正数表示向后加flag天，负数表示向前减flag天。
     *
     * @param mon
     * @param flag
     * @return
     */
    public static Long lastDay(Long mon, int flag) {
        Date date = mon == null ? new Date() : new Date(mon);//当前日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);//设置当前日期
        calendar.add(Calendar.DAY_OF_MONTH, flag);
        return calendar.getTime().getTime();
    }

}
